﻿using System;
using System.IO;
namespace Ledict.ConditionExtract.Common
{
    public class AddLog
    {


        public static void WriteLog(Exception ex, string logAddress)
        {
            //如果日志文件为空，则默认在Debug目录下新建 YYYY-mm-dd_Log.log文件
            if (logAddress == "")
            {
                logAddress = Environment.CurrentDirectory + '\\' +
                    DateTime.Now.Year + '-' +
                    DateTime.Now.Month + '-' +
                    DateTime.Now.Day + "_Log.log";
            }

            //把异常信息输出到文件
            var fs = new StreamWriter(logAddress, true);
            fs.WriteLine("当前时间：" + DateTime.Now.ToString());
            fs.WriteLine("异常信息：" + ex.Message);
            fs.WriteLine("异常对象：" + ex.Source);
            fs.WriteLine("调用堆栈：\n" + ex.StackTrace.Trim());
            fs.WriteLine("触发方法：" + ex.TargetSite);
            fs.WriteLine();
            fs.Close();
        }
        public static void AgainDataWriteLog(string ex, string ErrorReasion)
        {
            //如果日志文件为空，则默认在Debug目录下新建 YYYY-mm-dd_Log.log文件

            string logAddress = Environment.CurrentDirectory + '\\' +
                 DateTime.Now.Year + '-' +
                 DateTime.Now.Month + '-' +
                 DateTime.Now.Day + "_Log.log";


            //把异常信息输出到文件
            var fs = new StreamWriter(logAddress, true);
            fs.WriteLine("当前时间：" + DateTime.Now.ToString());
            fs.WriteLine("异常信息：" + ex);
            fs.WriteLine("错误：" + ErrorReasion);

            fs.WriteLine();
            fs.Close();
        }

    }
}
